package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.service.DeptService;
import com.itheima.utils.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/depts")
public class DeptController {

    private final DeptService deptService;

    @GetMapping
    public Result queryAllDepts() {
        List<Dept> depts = deptService.queryAllDepts();
        return Result.success(depts);
    }

    @PostMapping
    public Result addDept(@RequestBody Dept dept) {
        log.info("执行了添加操作");
        deptService.addDept(dept);
        return Result.success("添加成功");
    }

    @PutMapping
    public Result updateDept(@RequestBody Dept dept) {
        log.info("执行了修改操作");
        deptService.updateDept(dept);
        return Result.success("修改成功");
    }

    //根据id查询
    @GetMapping("/{id}")
    public Result queryDeptById(@PathVariable Integer id) {
        Dept dept = deptService.queryDeptById(id);
        return Result.success(dept);
    }

    @DeleteMapping
    public Result deleteDeptById(Integer id) {
        deptService.deleteDeptById(id);
        return Result.success("删除成功");
    }
}
